<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <Article-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="datasource" :selection.sync="selection">
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit()">
            新建
          </el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" class="ele-btn-icon" @click="removeBatch">
            删除
          </el-button>
        </template>

        <!-- 图片列 -->
        <template v-slot:thumb="{ row }">
          <img :src="row.thumb" style="width: 50px; height: 50px; " />
        </template>

        <!-- 推荐列 -->
        <template v-slot:flag="{ row }">
          <el-tag v-if="row.flag == 0" type="info">未推荐</el-tag>
          <el-tag v-if="row.flag == 1" type="success">已推荐</el-tag>
        </template>


        <!-- 状态列 -->
        <template v-slot:statusFlag="{ row }">
          <el-switch :active-value="0" :inactive-value="1" v-model="row.statusFlag" @change="editStatus(row)" />
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <!-- 如果已经推荐 显示取消 flag !=0 -->
          <el-button v-if="row.flag != 0" icon="el-icon-star-on" type="text"
            @click="openRecommendCancle(row)">取消推荐</el-button>
          <el-button v-else icon="el-icon-star-off" type="text" @click="openRecommendCancle(row)">推荐</el-button>
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)">
            修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此用户吗？" @confirm="remove(row)">
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>


    <!-- dialog 弹窗推荐 -->
    <el-dialog title="推荐" :visible.sync="dialogVisible" width="500px">
      <el-form :model="recommendForm" :rules="recommendRules" ref="recommendForm" label-width="80px">
        <el-form-item label="" prop="recImg">
          <upload-img class="logo" :type="uploadRules" v-model="recommendForm.recImg" :info="logoText"></upload-img>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="recommend">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import ArticleSearch from './components/article-search.vue';
import uploadImg from "@/components/upload-img";
import {
  pageArticles,
  removeArticle,
  removeArticles,
  updateArticleStatus,
  recommendArticle
} from '@/api/article';

export default {
  name: 'ArticleIndex',
  components: {
    ArticleSearch,
    uploadImg
  },
  data() {
    return {
      // 表格列配置  
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          align: 'center'
        },
        {
          slot: 'thumb',
          prop: 'thumb',
          label: '图片',
          align: 'center',
        },
        {
          prop: 'title',
          label: '标题',
          align: 'center',
        },
        // sources 来源
        {
          prop: 'sources',
          label: '来源',
          align: 'center',
        },
        {
          prop: 'showTime',
          align: 'center',
          label: '显示时间',
        },
        {
          slot: 'statusFlag',
          prop: 'statusFlag',
          label: '状态',
          align: 'center',
        },
        {
          slot: 'flag',
          prop: 'flag',
          label: '推荐',
          align: 'center',
        },
        {
          columnKey: 'action',
          label: '操作',
          align: 'center',
          resizable: false,
          slot: 'action',
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false,
      recommendForm: {
        id: 0,
        flag: 1,
        recImg: ''
      },
      recommendRules: {
        recImg: [{ required: true, message: "请上传图片", trigger: "blur" }]
      },
      dialogVisible: false,
      uploadRules: ["image/jpeg", "image/jpg", "image/png"],
      logoText: "请上传推荐图片",
    };
  },
  methods: {
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return pageArticles({ ...where, ...order, page, limit });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      // this.current = row;
      // 跳转到编辑页面
      this.$router.push({
        path: "./edit",
        query: { id: row ? row.id : '' },
      });
    },
    /* 打开导入弹窗 */
    openImport() {
      this.showImport = true;
    },
    /* 推荐 */
    openRecommend(row) {
      // this.dialogVisible = true;
      this.recommendForm.id = row.id;
    },
    //openRecommendCancle 取消推荐
    openRecommendCancle(row) {
      let msg = row.flag == 0 ? '确定要推荐吗?' : '确定要取消推荐吗?';
      this.$confirm(msg, '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          recommendArticle({ id: row.id, flag: 0 })
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      removeArticle(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的资讯吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          removeArticles(this.selection.map((d) => d.ArticleId))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      updateArticleStatus(row.id, row.statusFlag)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
        })
        .catch((e) => {
          loading.close();
          row.statusFlag = !row.statusFlag ? 1 : 0;
          this.$message.error(e.message);
        });
    },
    // 推荐
    recommend() {
      this.$refs.recommendForm.validate((valid) => {
        if (valid) {
          const loading = this.$loading({ lock: true });

          recommendArticle(this.recommendForm)
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.dialogVisible = false;
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
  }
};
</script>
